package cn.itcast.zuul.filters;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: taft
 * @Date: 2018-12-12 9:39
 */
@Component
public class PaiziFilter extends ZuulFilter {
    @Override
    public String filterType() {//pre routing post error
        return "pre";
    }

    @Override
    public int filterOrder() {//过滤器在过滤器列表的执行次序
        return 80;
    }

    @Override
    public boolean shouldFilter() {//是否启用过滤器，true，启用
        return true;
    }

    @Override
    public Object run() throws ZuulException {//过滤器的执行逻辑，拦截要操作，放行不用管

        //获取当前请求的资源管理器
        RequestContext currentContext = RequestContext.getCurrentContext();

        //获取当前的请求对象
        HttpServletRequest request = currentContext.getRequest();

        String paizi = request.getParameter("paizi");

        if (StringUtils.isEmpty(paizi)){
            //拦截就是不响应
            currentContext.setSendZuulResponse(false);
            //礼貌的给一个状态码,200,201,204,302,400,401,404,403,405,500,502,
            currentContext.setResponseStatusCode(401);

        }


        return null;
    }
}
